GBML manual / Link Cable

The link cable

Two Game Boy Advances joined by a link cable can share their buttons, so a game can have a second player on a second console - and it carries on safely when the cable is not there.

What the cable carries

A Game Boy Advance link cable joins two consoles, or up to four with the right cable. Each console can send the other a message, once in a while, and a message is one small number. That is all. There is no way to send a picture, a word or a list, and a message is allowed to get lost on the way.

A game cannot do much with "send a number". So GBML does not ask you to. It shares the one thing nearly every two-player game needs: which buttons each player is pressing.

Every console sends its own buttons, every frame, to everyone on the cable. In return, your game can ask about any player's buttons the same way it asks about its own:

Your own buttonsAny player on the cable
btn_held(BTN_A)link_button_held(2, BTN_A)
btn_pressed(BTN_A)link_button_pressed(2, BTN_A)
btn_released(BTN_A)link_button_released(2, BTN_A)
btn_dx() and btn_dy()link_dx(2) and link_dy(2)

The number in front is the player: 1 to 4. Asking about your own player number gives you your own buttons, with no delay.

Switching it on

Call link_start once. The Create event of your main object is a good place. It is safe to call again later: a second call does nothing.

// Create event
link_start();

Until you do, the link cable is off and costs nothing at all. A game that never calls link_start is exactly the game it was before the link cable existed.

Two players, one piece of code

Both consoles run the same game. Each one keeps both players' characters and moves them. The only thing that crosses the cable is the buttons. Look at what that does to the code:

// Step event of a square on both screens
if (link_connected()) {
    x += link_dx(1) * 2;
    y += link_dy(1) * 2;
}

link_dx(1) means "player 1's d-pad, left or right". On the console that is player 1, it reads its own d-pad. On the other console, it reads what player 1 was pressing a moment ago. The code never asks which console it is running on, and it never sends anything. The second square does the same with link_dx(2).

Any rule you build out of buttons works the same way. Here the second player fires when their A is pressed, on both consoles:

// Step event of player 2's gun
if (link_button_pressed(2, BTN_A)) {
    x += 1;
}

Who is player 1?

A console learns its place on the cable from the first message it receives. link_player tells you: 1 to 4, or 0 if it does not know yet. Player 1 is the console with the cable's small purple plug in it. The other plug, the larger gray one, goes in the other console.

Both consoles run the same code, so this is how a game puts the first player's ship on the left of one screen and the second player's on the left of the other. link_players says how many consoles are playing, counting this one.

// Draw GUI event
var me = link_player();
draw_text(8, 8, $"YOU ARE PLAYER {me}");

When nobody is there

A game that waited for the cable would freeze when someone forgot to plug it in. This one never waits. Switching the link on takes no time, nothing stops for it, and the game runs at full speed whether or not anybody is on the other end. Ask link_connected:

So the first thing a two-player game does is look at link_connected(), and draw a "waiting for the other player" screen until it is true. The example project does exactly that.

When someone leaves

Every console sends a message every frame, even when it is not pressing anything. That is the rule that makes everything else safe. A console that is quiet but connected is still talking. A console that has gone quiet entirely is gone.

If nothing has been heard from a player for 30 frames, half a second, the game decides they have gone: the cable was pulled, the other console was switched off, or it left the two-player part of the game. Three things happen:

  1. Their buttons are let go of. link_button_released is true for them, once, on that frame, and from then on every question about them is "no". A cable pulled while a player was holding RIGHT can never leave RIGHT stuck down.
  2. link_connected turns false if they were the last player, and link_player() goes back to 0.
  3. If they come back, they are treated as brand new, and the cable works again with no code from you.

Nothing froze, nothing waited, and the game decides what a missing player means. The example pauses itself until the cable is back. A game could instead carry on alone, or go back to the title screen.

What it cannot do

This is the part to read before you plan a game.

Only buttons cross the cable. There is no way to send a score, a position, a health bar or a random number. Each console works those out for itself.

The two consoles never agree to the exact frame. The other player's buttons arrive about one to two frames after they were pressed, so each console is always a tiny bit behind about the other player. A message can also be lost, and a lost message is a frame of someone's buttons the game never sees. Over a long game two screens that started the same can drift: a square that is two pixels further left on one console than on the other.

So some things work well and some do not.

Works wellDoes not work
Characters that move by pressing buttonsA score both players must agree on to the last point
Two players in the same room, each seeing the other moveAnything that uses a random number: each console picks its own, so enemies appear in different places
Short games, or games with a rule that puts everyone back together (the example's START)A long game where a pixel of drift would decide who wins
A menu where both players choose togetherHits that count on one console and miss on the other

link_missed counts the messages that never arrived. Draw it on the screen while you test. If it climbs, the cable or the distance is the problem. If it stays at 0 and the screens still drift, the lag is the cause, and the answer is a rule that brings both back together.

What it costs

A link cable is not free. Measured on an emulated console with nobody plugged in:

A game that never calls link_start pays none of it, and its ROM is the same ROM it was before the link cable was added.

What has and has not been tested

The software around the cable is tested on an emulated console: who is player 1, held, pressed and released for another player, a quick tap, lost messages being counted, a player going quiet being let go of after exactly 30 frames, a player returning, a long stall, and a console with nobody on the cable never connecting and never waiting. Those tests play two consoles inside one program over a pretend cable, so they prove the bookkeeping and the way it talks to the console's link code. The real link layer of the engine is also run with nobody plugged in, which is the situation of anyone without a cable.

Nothing here has been run on two real consoles, or on two emulator windows, with real messages crossing a real link. A link cable can fail in ways a pretend one cannot: a loose plug, a long cable, a console that is slower than its partner. If you find one, link_missed is the first thing to look at.

Things to know

See also: link_startlink_connectedlink_playerlink_button_heldlink_dxReading the buttons